package com.aviccxzx.smarthome.service;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.google.gson.GsonBuilder;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by win764-1 on 2016/12/12.
 */

public class RetrofitHelper {

    private Context mCntext;

    OkHttpClient client = new OkHttpClient();
    GsonConverterFactory factory = GsonConverterFactory.create(new GsonBuilder().create());
    private static RetrofitHelper instance = null;
    private Retrofit mRetrofit = null;
    String mUrl = "http://aviconicshome.aviccxzx.com:8180/";

    public static RetrofitHelper getInstance(Context context) {
        if (instance == null) {
            instance = new RetrofitHelper(context);
        }
        return instance;
    }

    private RetrofitHelper(Context mContext) {
        mCntext = mContext;
        init();
    }

    private void init() {
        //        resetApp();
    }

    private void resetApp() {
        mRetrofit = new Retrofit.Builder()
                .baseUrl(mUrl+"gateway/")
                .client(client)
                .addConverterFactory(factory)
                .client(getOkHttpClient())
                .build();
    }

    public RetrofitService getServer() {
        mRetrofit = new Retrofit.Builder()
                .baseUrl(mUrl+"gateway/")
                .client(client)
                .addConverterFactory(factory)
                .client(getOkHttpClient())
                .build();
        return mRetrofit.create(RetrofitService.class);
    }

    public RetrofitService getLoginServer() {
        mRetrofit = new Retrofit.Builder()
                .baseUrl(mUrl+"gateway/")
                .client(client)
                .addConverterFactory(factory)
                .client(getOkHttpClient1())
                .build();
        return mRetrofit.create(RetrofitService.class);
    }

    public RetrofitService getStatusServer(Interceptor addHeader) {
        mRetrofit = new Retrofit.Builder()
                .baseUrl(mUrl+"gateway/")
                .client(client)
                .addConverterFactory(factory)
                .client(getOkHttpClientStatus(addHeader))
                .build();
        return mRetrofit.create(RetrofitService.class);
    }

    private OkHttpClient getOkHttpClient() {
        //日志显示级别
        HttpLoggingInterceptor.Level level = HttpLoggingInterceptor.Level.BODY;
        //新建log拦截器
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.d("zcb", "OkHttp====Message:" + message);
            }
        });
        loggingInterceptor.setLevel(level);

        SharedPreferences spf = mCntext.getSharedPreferences("set", Context.MODE_PRIVATE);
        String token = spf.getString("loginToken", "");
        final String authToken = "bearer " + token;
        //添加请求头拦截器
        Interceptor addHeader = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request()
                        .newBuilder()
                        .addHeader("Authorization", authToken)
                        .build();
                return chain.proceed(request);
            }
        };

        //定制OkHttp
        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
        //OkHttp进行添加拦截器loggingInterceptor
        httpClientBuilder.addInterceptor(loggingInterceptor);
        //OKHttp进行添加addHeader拦截器
        httpClientBuilder.addInterceptor(addHeader);
        return httpClientBuilder.build();
    }

    private OkHttpClient getOkHttpClient1() {
        //日志显示级别
        HttpLoggingInterceptor.Level level = HttpLoggingInterceptor.Level.BODY;
        //新建log拦截器
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.d("zcb", "OkHttp====Message:" + message);
            }
        });
        loggingInterceptor.setLevel(level);

        //定制OkHttp
        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
        //OkHttp进行添加拦截器loggingInterceptor
        httpClientBuilder.addInterceptor(loggingInterceptor);
        return httpClientBuilder.build();
    }

    private OkHttpClient getOkHttpClientStatus(Interceptor addHeader) {
        //日志显示级别
        HttpLoggingInterceptor.Level level = HttpLoggingInterceptor.Level.BODY;
        //新建log拦截器
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.d("zcb", "OkHttp====Message:" + message);
            }
        });
        loggingInterceptor.setLevel(level);

        //定制OkHttp
        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
        //OkHttp进行添加拦截器loggingInterceptor
        httpClientBuilder.addInterceptor(loggingInterceptor);
        //OKHttp进行添加addHeader拦截器
        httpClientBuilder.addInterceptor(addHeader);
        return httpClientBuilder.build();
    }
}
